const connection = require('../app/database')

class ChargeService {
    async create(name,phone,address,age,sex,remark) {
        const statement = `INSERT INTO charge (name,phone,address,age,sex,remark) VALUES (?,?,?,?,?,?);`
        const result = await connection.execute(statement,[name,phone,address,age,sex,remark])
        return result
    }
    async getListCount() {
        const statement = `SELECT COUNT(*) AS totalCount from charge;`
        const [result] = await connection.execute(statement)
        return result[0]
    }
    async getList(pageSize,offset) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
        console.log("pageSize,offset",pageSize,offset);
        const statement = `select * from charge order by id desc limit ${pageSize} offset ${offset};`;
        const [result] = await connection.execute(statement);
        return result
    }
    async edit(id,name,phone,address,age,sex,remark) {
        console.log("id,name,phone,address,age,sex,remark",id,name,phone,address,age,sex,remark);
        const statement = `UPDATE charge SET 
                                name = ?,
                                phone = ?,
                                address = ?,
                                age = ?,
                                sex = ?,
                                remark = ?
                            WHERE id = ?;`
        const result = await connection.execute(statement,[name,phone,address,age,sex,remark,id]) 
        return result
    }
    async remove(id) {
        const statement = `DELETE FROM charge WHERE id = ?;`
        const result = await connection.execute(statement,[id])
        return result
    }
}

module.exports = new ChargeService()